﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using System.Text;
using com.loto.Common;

namespace com.huigo.Admin.Web.customer
{
    public partial class SalesCustStatistics : BasePage
    {
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(fromDate.Text) ? Convert.ToDateTime(fromDate.Text) : DateTime.MinValue;
            }
        }
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(toDate.Text) ? Convert.ToDateTime(toDate.Text) : DateTime.MinValue;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }
        private void BindData()
        {
            if (queryEnd != DateTime.MinValue && queryStart != DateTime.MinValue)
            {
                if (queryEnd > queryStart || queryEnd == queryStart)
                {
                    if (queryEnd.AddMonths(-1) <= queryStart)
                    {
                        List<RequestFuncParams> list = new List<RequestFuncParams>();
                        list.Add(new RequestFuncParams("start", queryStart.ToString()));
                        list.Add(new RequestFuncParams("end", queryEnd.AddDays(1).ToString()));
                        list.Add(new RequestFuncParams("userId", UserId.ToString()));
                        BaseService service = new BaseService();
                        ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_SALES_RELATION, list);
                        if (parser != null && parser.RowsCount > 0)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("<table>");
                            sb.AppendLine("<thead>");
                            sb.AppendLine("<tr>");
                            sb.AppendLine("<th>");
                            sb.AppendLine("序号");
                            sb.AppendLine("</th>");
                            sb.AppendLine("<th>");
                            sb.AppendLine("经销商");
                            sb.AppendLine("</th>");
                            sb.AppendLine("<th>");
                            sb.AppendLine("金额");
                            sb.AppendLine("</th>");
                            sb.AppendLine("<th>");
                            sb.AppendLine("操作");
                            sb.AppendLine("</th>");
                            sb.AppendLine("</tr>");
                            sb.AppendLine("</thead>");
                            sb.AppendLine("<tbody>");
                            for (int i = 0; i < parser.RowsCount; i++)
                            {
                                sb.AppendLine("<tr>");
                                sb.AppendLine("<td>");
                                sb.AppendLine((i + 1).ToString());
                                sb.AppendLine("</td>");
                                sb.AppendLine("<td>");
                                sb.AppendLine(parser["CustName", i]);
                                sb.AppendLine("</td>");
                                sb.AppendLine("<td>");
                                sb.AppendLine(string.Format("{0:C2}", Convert.ToDecimal(parser["Price", i])));
                                sb.AppendLine("</td>");
                                sb.AppendLine("<td>");
                                sb.AppendLine("<a href=\"javascript:GetSales('" + parser["CustFid", i] + "','" + Convert.ToDateTime(queryStart).ToString("yyyy-MM-dd") + "','" + Convert.ToDateTime(queryEnd).ToString("yyyy-MM-dd") + "')\">查看明细</a>");
                                sb.AppendLine("</td>");
                                sb.AppendLine("</tr>");
                            }
                            sb.AppendLine("</tbody>");
                            sb.AppendLine("</table>");
                            litSettlement.Text = sb.ToString();
                        }
                        else
                        {
                            litSettlement.Text = string.Empty;
                        }
                    }
                    else
                    {
                        MessageBox.Show(this.Page, "时间范围不能超过一个月");
                        litSettlement.Text = string.Empty;
                        return;
                    }
                }
                else
                {
                    MessageBox.Show(this.Page, "结束时间要大于开始时间,，重新选择时间");
                    litSettlement.Text = string.Empty;
                    return;
                }
            }
        }
    }
}
